# PowerShell script to start frontend development server
# This script bypasses execution policy issues

Write-Host "🚀 Starting n8ncrm Frontend Development Server..." -ForegroundColor Green

# Change to frontend directory
$frontendPath = "D:\n8ncrm\frontend"
Set-Location $frontendPath

Write-Host "📁 Current directory: $(Get-Location)" -ForegroundColor Blue

# Check if package.json exists
if (-not (Test-Path "package.json")) {
    Write-Host "❌ package.json not found in frontend directory!" -ForegroundColor Red
    Write-Host "Please ensure you're in the correct directory: $frontendPath" -ForegroundColor Yellow
    exit 1
}

# Check if node_modules exists and has .bin directory
if (-not (Test-Path "node_modules\.bin")) {
    Write-Host "📦 Installing dependencies..." -ForegroundColor Yellow
    npm install
    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ Failed to install dependencies!" -ForegroundColor Red
        exit 1
    }
}

# Check if vite is available
if (-not (Test-Path "node_modules\.bin\vite.cmd")) {
    Write-Host "❌ Vite not found in node_modules/.bin!" -ForegroundColor Red
    Write-Host "Trying to reinstall dependencies..." -ForegroundColor Yellow
    npm install
}

# Start development server
Write-Host "🌐 Starting Vite development server..." -ForegroundColor Cyan
npm run dev
